package org.geekbang.thinking.in.spring.bean.definition;

import org.geekbang.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * bean alias
 *
 * @author wangyh2
 * @version v1.0
 * @since 2021-09-13 17:31
 */
public class BeanAliasDemo {
    public static void main(String[] args) {
        // 配置 XML 配置文件
        // 启动 Spring 应用上下文
        // 说白了, ClassPathXmlApplicationContext实现了BeanFactory接口
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/bean-definitions-context.xml");
        User user = beanFactory.getBean("user", User.class);
        User sishenUser = beanFactory.getBean("sishenUser", User.class);
        System.out.println("sishenUser == user: " + (sishenUser == user));

        String[] sishenUsers = beanFactory.getAliases("sishenUser");
        System.out.println("通过别名获取beans: ");
        for (String s : sishenUsers) {
            System.out.println(s.toString());
        }
    }
}
